<template>
  <div class="goods-add">
    <Pane>
      <span slot="title">商品添加</span>
      <section slot="content">
        <el-form :model="goodsAddForm">
          <!-- 商品名称 -->
          <el-form-item label="商品名称">
            <el-input v-model="goodsAddForm.name"></el-input>
          </el-form-item>

          <!-- 商品分类 -->
          <el-form-item label="商品分类">
            <el-select v-model="goodsAddForm.category">
              <el-option
                v-for="cate in categories"
                :key="cate.cateName"
                :value="cate.cateName"
              >{{cate.cateName}}</el-option>
            </el-select>
          </el-form-item>

          <!-- 商品价格 -->
          <el-form-item label="商品价格">
            <el-input-number v-model="goodsAddForm.price" :min="0" :max="10000"></el-input-number>
          </el-form-item>

          <!-- 商品图片 -->
          <el-form-item label="商品图片">
            <el-upload
              class="avatar-uploader"
              action="http://127.0.0.1:5000/goods/goods_img_upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="goodsAddForm.imgUrl" :src="imgBaseUrl+goodsAddForm.imgUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <!-- 商品描述 -->
          <el-form-item label="商品描述">
            <el-input v-model="goodsAddForm.goodsDesc" type="textarea" :rows="5"></el-input>
          </el-form-item>

          <!-- 增加商品 按钮-->
          <el-form-item>
            <el-button type="success" size="small" @click="submitGoodsAddBtn">添加商品</el-button>
          </el-form-item>
        </el-form>
      </section>
    </Pane>
  </div>
</template>

<script>
// 引入面板
import Pane from "@/components/Pane/Pane";
// 获取数据接口
import {
  getGoodsAdd,
  getGoodsImgUpload,
  getGoodsCategories,
} from "@/api/goods";

export default {
  // 注册
  components: {
    Pane,
  },
  data() {
    return {
      goodsAddForm: {
        name: "",
        category: "",
        price: 0,
        imgUrl: "",
        goodsDesc: "",
      },
      categories: [],
      imgBaseUrl: "http://127.0.0.1:5000/upload/imgs/goods_img/",
    };
  },
  created() {
    this.init();
  },
  methods: {
    handleAvatarSuccess(res) {
      let { code, msg, imgUrl } = res;
      if (code === 0) {
        this.$message.success(msg);
        this.goodsAddForm.imgUrl = imgUrl;
      }
    },
    // 上传图片
    beforeAvatarUpload(file) {
      console.log(file);
      // 图片类型
      /* 
      name: "e836d817685fb2cb12c4fe77219e2a9f.jpeg"
      size: 225473
      type: "image/jpeg"
      uid: 1596164570252
      webkitRelativePath: ""
      */
      //  ==  和 === 区别？？
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/gif" ||
        file.type === "image/bmp" ||
        file.type === "image/webp";

      const isLt4M = file.size / 1024 / 1024 < 4; // 限制大小
      if (!isJPG) {
        this.$message.error("上传的图片只能是有jpeg,jpg,png,gif,bmp,webp");
      }
      if (!isLt4M) {
        this.$message.error("上传的图片最大是4M");
      }
      return isJPG && isLt4M;
    },
    async init() {
      let { categories } = await getGoodsCategories();
      console.log(categories);
      this.categories = categories;
    },
    async submitGoodsAddBtn() {
      let { code, msg } = await getGoodsAdd(this.goodsAddForm);
      console.log(code);
      if (code === 0) {
        this.$message.success(msg);
        this.$router.push("/goods/goods-list");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.el-form-item__content {
  display: flex;
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
/deep/ .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>